import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import set from '@dfeidao/atom-web/local/set';
import storage_put from '@dfeidao/atom-web/storage/put';

import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';

// import XL from '@dfeidao/fd-w000018';
import P01 from './P01';
import P02 from './P02';
import P03 from './P03';
import P04 from './P04';

export default async function a001(fd: IFeidaoAiBrowserComponent) {
	// 初始化

	const productid = fd.data.node.querySelector<HTMLSpanElement>('[data-feidao-id="user"]').getAttribute('prid');

	set(fd, 'productid', productid);
	await storage_put('productid', productid);

	const page_no = 1;
	const page_size = 10;
	const asc = new Array();
	const desc = new Array();
	asc.push('tenantid');

	const res1 = await nodejs<{ dataArray: Array<{}> }>('tenant-inf/zj-000001/s002', { filter: { productid } });

	render(fd, res1.dataArray, P04, 'P04', 'inner');
	// fd.data.node.querySelector<XL>('[id="xiala1"]').set_selected('');

	const res = await nodejs<{ dataArray: Array<{}>, count: number, page_no: number; page_size: number }>('tenant-inf/zj-000001/s001', { filter: { productid, is_disable: false }, page_size, page_no, asc, desc });
	const page = {
		page_no: res.page_no,
		page_size: res.page_size,
		count: res.count
	};
	// debugger;
	if (res.count === 0) {
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P01"]').classList.add('display-n'); // 数据
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P02"]').classList.remove('display-n'); // 空模板
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P03"]').classList.add('display-n'); // 分页
		// fd.data.node.querySelector<HTMLDivElement>('[data-feidao-name="selpage"]').classList.add('display-n');
		render(fd, '', P02, 'P02', 'inner');
	} else if (res.count < 10) {
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P01"]').classList.remove('display-n');
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P02"]').classList.add('display-n');
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P03"]').classList.add('display-n');
		render(fd, res.dataArray, P01, 'P01', 'inner');
		// fd.data.node.querySelector<HTMLDivElement>('[data-feidao-name="selpage"]').classList.remove('display-n');
	} else {
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P01"]').classList.remove('display-n');
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P02"]').classList.add('display-n');
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P03"]').classList.remove('display-n');
		render(fd, res.dataArray, P01, 'P01', 'inner');
		render(fd, page, P03, 'P03', 'inner');
	}
}
